package sivin.uikit.framework

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import androidx.viewpager2.widget.ViewPager2

open class BindingFragment<B : ViewBinding>(
    private val inflater: (LayoutInflater) -> B
) : Fragment() {
    protected val binding: B by lazy {
        inflater(layoutInflater)
    }

    protected open fun expandStatusBar(): Boolean = false

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            val top = if (expandStatusBar()) {
                0
            } else {
                systemBars.top
            }
            v.setPadding(systemBars.left, top, systemBars.right, systemBars.bottom)
            insets
        }
        return binding.root
    }
}